package com.sls.game.client;

import java.net.InetSocketAddress;
import java.nio.charset.Charset;

import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
import org.apache.mina.core.filterchain.IoFilter;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.transport.socket.nio.NioSocketConnector;

public class MinaClient
{
	public static void main( String[] args ) throws Exception
	{
		// create tcp/ip connector
		NioSocketConnector connector = new NioSocketConnector();
		// 创建接收数据的过滤器
		DefaultIoFilterChainBuilder chain = connector.getFilterChain();
		TextLineCodecFactory lineCodec = new TextLineCodecFactory( Charset.forName( "utf-8" ) );
		lineCodec.setDecoderMaxLineLength( 1024 * 1024 ); // 1M
		lineCodec.setEncoderMaxLineLength( 1024 * 1024 ); // 1M
		IoFilter filter = new ProtocolCodecFilter( lineCodec );
		// 设定这个过滤器将一行一行读数据
		chain.addLast( "test", filter );
		connector.setHandler( new MinaClientHandler() );
		connector.setConnectTimeoutMillis( 30000 );
		// 连接到服务器
		ConnectFuture cf = connector.connect( new InetSocketAddress( "127.0.0.1", 10000 ) );
		// wait for the connection attempt to be finished
		cf.awaitUninterruptibly();
		cf.getSession().getCloseFuture().awaitUninterruptibly();
		connector.dispose();
	}
}
